<?php

namespace App\Console\Commands\Cruise;

use App\Models\Cruise\CruiseCheckAgainstModel;
use App\Models\Cruise\CruiseCheckRectifyModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckAgainstService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;

class CruiseCheckAgainstSendNotify extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:cruise_check_against_send_notify';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '违约函到期自动发送商家消息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 违约函临期提醒(警告)      到期前一天早上8点推送
     * 违约函临期提醒（处罚）     到期前一天早上8点推送
     * 违约函已过期提醒(处罚)     到期后第一天早上8点推送
     * 整改单到期提醒            到期前一天早上8点推送
     * @return int
     * @throws GuzzleException
     */
    public function handle()
    {

        //获取明天即将过期的违约函
        $tomorrowDate = date('Y-m-d', strtotime("+1 day"));  //明天时间
        $tomorrowTime = strtotime($tomorrowDate);            //明天时间戳
        CruiseCheckAgainstModel::query()
            ->where('status', CruiseCheckAgainstModel::STATUS_WAIT)//未处理
            ->where('endTime', '>=', $tomorrowTime)
            ->where('endTime', '<', $tomorrowTime + 86400)
            ->chunk(100, function ($item) {
                foreach ($item as $v) {
                    $type = 0;//1:违约函生成提醒 2:违约函临期提醒(警告) 3:违约函临期提醒（处罚） 4:违约函已过期提醒(处罚)
                    if ($v['level'] == CruiseCheckAgainstModel::LEVEL_WARNING) {//警告
                        $type = CruiseCheckAgainstService::WARN_CRUISE_CHECK_AGAINST;
                    } elseif ($v['level'] == CruiseCheckAgainstModel::LEVEL_PUNISH) {//处罚
                        $type = CruiseCheckAgainstService::PUNISH_CRUISE_CHECK_AGAINST;
                    }
                    if ($type > 0) {
                        CruiseCheckAgainstService::sendSystemNotify($v['shopId'], $v['endTime'], $type, $v['id'], $v['level']);
                    }
                }
            });

        //获取明天即将过期的整改单
        CruiseCheckRectifyModel::query()
            ->where('templateType', CruiseTemplateModel::TYPE_3)
            ->where('status', CruiseCheckRectifyModel::STATUS_WAIT_SUBMIT)//待整改
            ->whereIn('lastAuditResult', [CruiseCheckRectifyModel::AUDIT_RESULT_FAIL, CruiseCheckRectifyModel::AUDIT_RESULT_INIT])// 未提交、不合格
            ->where('auditEndTime', '>=', $tomorrowTime)
            ->where('auditEndTime', '<', $tomorrowTime + 86400)
            ->selectRaw('id,shopId,cruiseCheckDetailId,auditEndTime,count(1) num')
            ->groupBy('cruiseCheckDetailId')
            ->chunk(100, function ($item) {
                foreach ($item as $v) {
                    if ($v['num'] > 0) {
                        CruiseCheckAgainstService::sendSystemNotify($v['shopId'], $v['auditEndTime'], CruiseCheckAgainstService::PUNISH_CRUISE_CHECK_RECTIFY_EXPIRE, 0, 0, $v['num']);
                    }
                }
            });

        //获取昨天过期的处罚-违约函
        $yesterdayDate = date('Y-m-d', strtotime("-1 day")); //昨天时间
        $yesterdayTime = strtotime($yesterdayDate);          //昨天时间戳
        CruiseCheckAgainstModel::query()
            ->where('status', CruiseCheckAgainstModel::STATUS_WAIT)//未处理
            ->where('level', CruiseCheckAgainstModel::LEVEL_PUNISH)//处罚
            ->where('endTime', '>=', $yesterdayTime)
            ->where('endTime', '<', $yesterdayTime + 86400)
            ->chunk(100, function ($item) {
                foreach ($item as $v) {
                    //4-违约函已过期提醒(处罚)
                    CruiseCheckAgainstService::sendSystemNotify($v['shopId'], $v['endTime'], CruiseCheckAgainstService::PUNISH_CRUISE_CHECK_AGAINST_EXPIRE, $v['id'], $v['level']);
                }
            });

    }
}
